package com.yoki.loginview;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.nbpt.smarthomelibrary.SmartHub;
import com.yoki.loginview.Save.SaveString;

import java.util.Timer;
import java.util.TimerTask;

public class SmartJiaJuActivity extends AppCompatActivity {

    private TextView TempTV;
    private TextView LightTV;
    private ImageView fan;
    private int[] imageIds = new int[]{
            R.drawable.f1,
            R.drawable.f2,
            R.drawable.f3,
            R.drawable.f4,
            R.drawable.f5,
            R.drawable.f6,
            R.drawable.f7,
            R.drawable.f8
    };
    private int num = 0;
    private float temp = 0;
    private Handler headler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == SaveString.MSG_DISPLAY_TEMPERATURE)
            {
                TempTV.setText(String.valueOf(msg.obj)+"℃");
                temp = Float.parseFloat(String.valueOf(msg.obj));
            }
            else if(msg.what == SaveString.MSG_DISPLAY_ILLUMINATION)
            {
                LightTV.setText(String.valueOf(msg.obj)+"lx");
            }
            else if(msg.what == SaveString.MSG_DISPLAY_IMAGEFAN)
            {
                if(temp >= 35)
                {
                    fan.setImageResource(imageIds[num++]);
                    if(num>=8)
                    {
                        num=0;
                    }
                }
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smartjiaju);

        TempTV = findViewById(R.id.Temp);
        LightTV = findViewById(R.id.Light);
        fan = findViewById(R.id.FanImage);

        TempTV.setText("0.0℃");
        LightTV.setText("0.0lx");

        Thread temp = new Thread(){
            @Override
            public void run() {
                super.run();
                while (true)
                {
                    try
                    {
                        SmartHub hub = new SmartHub();
                        float temp= hub.getRoomTemperature();
                        Message msg = Message.obtain();
                        msg.what = SaveString.MSG_DISPLAY_TEMPERATURE;
                        msg.obj = temp;
                        Log.d("线程"+String.valueOf(android.os.Process.myTid()),"温度为:"+String.valueOf(temp));
                        headler.sendMessage(msg);
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        };
        temp.start();

        Thread light = new Thread(){
            @Override
            public void run() {
                super.run();
                while (true)
                {
                    try
                    {
                        SmartHub hub = new SmartHub();
                        float light= hub.getRoomIllumination();
                        Message msg = Message.obtain();
                        msg.what = SaveString.MSG_DISPLAY_ILLUMINATION;
                        msg.obj = light;
                        Log.d("线程"+String.valueOf(android.os.Process.myTid()),"光照为:"+String.valueOf(light));
                        headler.sendMessage(msg);
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        };
        light.start();

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = SaveString.MSG_DISPLAY_IMAGEFAN;
                headler.sendMessage(msg);
            }
        },0,100);
    }
}
